package cn.zh.stan.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cn.zh.stan.R;


/**
 * Created by WYN on 2018/1/15 0015.
 */

public class OrderFragment extends Fragment {

    private TabLayout tabLayout = null;

    private ViewPager viewPager;

    private Fragment[] mFragmentArrays = new Fragment[5];

    private String[] mTabTitles = new String[5];


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_order,container,false);
        inito(view);
        return view;
         }


      public void inito(View view){
          tabLayout = (TabLayout)view.findViewById(R.id.tablayout);
          viewPager = (ViewPager)view.findViewById(R.id.tab_viewpager);
          mTabTitles[0] = "全部";
          mTabTitles[1] = "待付款";
          mTabTitles[2] = "待发货";
          mTabTitles[3] = "已发货";
          mTabTitles[4] = "已完成";
          tabLayout.setTabMode(TabLayout.MODE_FIXED);
          //设置tablayout距离上下左右的距离
          //tab_title.setPadding(20,20,20,20);
          mFragmentArrays[0] = new AllGoods();
          mFragmentArrays[1] = new TPGooods();
          mFragmentArrays[2] = new SendGoods();
          mFragmentArrays[3] = new AlreadySend();
          mFragmentArrays[4] = new Completed();
          PagerAdapter pagerAdapter = new MyViewPagerAdapter(getActivity().getSupportFragmentManager());
          viewPager.setAdapter(pagerAdapter);
          //将ViewPager和TabLayout绑定
          tabLayout.setupWithViewPager(viewPager);
           }

    final class MyViewPagerAdapter extends FragmentPagerAdapter {
        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentArrays[position];
          }

        @Override
        public int getCount() {
            return mFragmentArrays.length;
          }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTabTitles[position];
         }
         }
        }
